package com.ev.asl.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ev.asl.constant.AslConstant;
import com.ev.asl.dto.AssemblyDaily2Dto;
import com.ev.asl.dto.AssemblyDailyDetailDtO;
import com.ev.asl.service.AssemblyDailyService;
import com.ev.asl.service.AssemblyProductionStatisticsService;
import com.ev.asl.vo.ActualSituation;
import com.ev.asl.vo.AssemblyProductionStatisticsVo;
import com.ev.asl.vo.BadForm;
import com.ev.asl.vo.TimeProfile;
import com.ev.framework.utils.DateUtils;
import com.ev.mes.domain.BomDO2;
import com.ev.mes.domain.CheckProjectDO;
import com.ev.mes.service.CheckProjectService;
import com.ev.mes.vo.WorkTimeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 姚家乐
 * @since 2021/6/23 15:00
 */
@Service
public class AssemblyProductionStatisticsServiceImpl implements
        AssemblyProductionStatisticsService {

    @Autowired
    private AssemblyDailyService assemblyDailyService;

    @Autowired
    private CheckProjectService checkProjectService;

    @Override
    public AssemblyProductionStatisticsVo apsAccounting(Map<String, Object> param) {
        List<Map<String, Object>> resultList = assemblyDailyService.listForMap(param);
        int total = assemblyDailyService.totalForMap(param);
        HashMap<String, Object> detailMap = new HashMap<>();
        AssemblyProductionStatisticsVo assemblyProductionStatisticsVo = new AssemblyProductionStatisticsVo();
        if (ObjectUtils.isEmpty(resultList)){
            return assemblyProductionStatisticsVo;
        }
        List<Long> ids = resultList.stream().
                filter(result -> !ObjectUtils.isEmpty(result.get("id")))
                .map(result -> new Long(result.get("id").toString()))
                .collect(Collectors.toList());
        detailMap.put("ids", ids);
        List<AssemblyDaily2Dto> detailList = assemblyDailyService.listDetail(detailMap);
        Map<Long, AssemblyDaily2Dto> detailMapById = detailList.stream().collect(Collectors.toMap(AssemblyDaily2Dto::getId, (v -> v), (oldValue, newValue) -> oldValue));

        assemblyProductionStatisticsVo.setTotal(total);
        //生产时间概况
        List<TimeProfile> timeProfiles = new ArrayList<>();

        //组装数量和
        BigDecimal totalMoldedQtyTotal = BigDecimal.ZERO;
        //套件不良数量和
        BigDecimal inspectionQtyTotal = BigDecimal.ZERO;
        //合格数量和
        BigDecimal qualifiedQtyTotal = BigDecimal.ZERO;
        //子件不良数量
        BigDecimal badItemValueTotal = BigDecimal.ZERO;
        //良品率
        BigDecimal yield = BigDecimal.ZERO;
        //出勤人数和
        BigDecimal attendanceTotal = BigDecimal.ZERO;

        //不良形态现况
        Map<String, BadForm> badFormMap = new HashMap<>();
        List<BadForm> badForms = new ArrayList<>();
        BadForm badForm = null;

        for (Map<String, Object> stringObjectMap : resultList) {
            TimeProfile timeProfile = new TimeProfile();
            //日期
            timeProfile.setDateString(DateUtils.format((Date) stringObjectMap.get("indicateDate")));
            timeProfile.setStartDate(DateUtils.format((Date) stringObjectMap.get("startDate"), DateUtils.DATE_TIME_PATTERN));
            timeProfile.setEndDate(DateUtils.format((Date) stringObjectMap.get("endDate"), DateUtils.DATE_TIME_PATTERN));

//            String workTimeMapJson = String.valueOf(stringObjectMap.get("workTimeMap"));
//            Map<String, Object> workTimeMap = JSON.parseObject(workTimeMapJson, HashMap.class);
//
            String workTimeMapJson = String.valueOf(stringObjectMap.get("workTimeMap2"));
//                Map<String, Object> workTimeMap = JSON.parseObject(workTimeMapJson, HashMap.class);
            List<WorkTimeVO> workTimeMap = JSON.parseArray(workTimeMapJson, WorkTimeVO.class);

            //  选项1-排料时间；选项2-清扫时间；选项3-维修时间；选项4-开会时间；选项5-培训时间
            if (ObjectUtil.isNotEmpty(workTimeMap)) {
                long zuoYe = workTimeMap.stream().filter(workTimeVO -> !ObjectUtils.isEmpty(workTimeVO.getValue1()) && !ObjectUtils.isEmpty(workTimeVO.getValue1().get(0)))
                        .map(workTimeVO -> DateUtil.between(workTimeVO.getValue1().get(0), workTimeVO.getValue1().get(1), DateUnit.MINUTE)).collect(Collectors.summarizingLong(time -> time)).getSum();
                long paiLiao = workTimeMap.stream().filter(workTimeVO -> !ObjectUtils.isEmpty(workTimeVO.getValue2()) && !ObjectUtils.isEmpty(workTimeVO.getValue2().get(0)) && !ObjectUtils.isEmpty(workTimeVO.getValuething()) && workTimeVO.getValuething().equals("选项1"))
                        .map(workTimeVO -> DateUtil.between(workTimeVO.getValue2().get(0), workTimeVO.getValue2().get(1), DateUnit.MINUTE)).collect(Collectors.summarizingLong(time -> time)).getSum();
                long qingSao = workTimeMap.stream().filter(workTimeVO -> !ObjectUtils.isEmpty(workTimeVO.getValue2()) && !ObjectUtils.isEmpty(workTimeVO.getValue2().get(0)) && !ObjectUtils.isEmpty(workTimeVO.getValuething()) && workTimeVO.getValuething().equals("选项2"))
                        .map(workTimeVO -> DateUtil.between(workTimeVO.getValue2().get(0), workTimeVO.getValue2().get(1), DateUnit.MINUTE)).collect(Collectors.summarizingLong(time -> time)).getSum();
                long xuanXiang = workTimeMap.stream().filter(workTimeVO -> !ObjectUtils.isEmpty(workTimeVO.getValue2()) && !ObjectUtils.isEmpty(workTimeVO.getValue2().get(0)) && !ObjectUtils.isEmpty(workTimeVO.getValuething()) && workTimeVO.getValuething().equals("选项3"))
                        .map(workTimeVO -> DateUtil.between(workTimeVO.getValue2().get(0), workTimeVO.getValue2().get(1), DateUnit.MINUTE)).collect(Collectors.summarizingLong(time -> time)).getSum();
                long kaiHui = workTimeMap.stream().filter(workTimeVO -> !ObjectUtils.isEmpty(workTimeVO.getValue2()) && !ObjectUtils.isEmpty(workTimeVO.getValue2().get(0)) && !ObjectUtils.isEmpty(workTimeVO.getValuething()) && workTimeVO.getValuething().equals("选项4"))
                        .map(workTimeVO -> DateUtil.between(workTimeVO.getValue2().get(0), workTimeVO.getValue2().get(1), DateUnit.MINUTE)).collect(Collectors.summarizingLong(time -> time)).getSum();
                long peiXun = workTimeMap.stream().filter(workTimeVO -> !ObjectUtils.isEmpty(workTimeVO.getValue2()) && !ObjectUtils.isEmpty(workTimeVO.getValue2().get(0)) && !ObjectUtils.isEmpty(workTimeVO.getValuething()) && workTimeVO.getValuething().equals("选项5"))
                        .map(workTimeVO -> DateUtil.between(workTimeVO.getValue2().get(0), workTimeVO.getValue2().get(1), DateUnit.MINUTE)).collect(Collectors.summarizingLong(time -> time)).getSum();
                long qiTa = workTimeMap.stream().filter(workTimeVO -> !ObjectUtils.isEmpty(workTimeVO.getValue2()) && !ObjectUtils.isEmpty(workTimeVO.getValue2().get(0)) && !ObjectUtils.isEmpty(workTimeVO.getValuething()) && workTimeVO.getValuething().equals("选项6"))
                        .map(workTimeVO -> DateUtil.between(workTimeVO.getValue2().get(0), workTimeVO.getValue2().get(1), DateUnit.MINUTE)).collect(Collectors.summarizingLong(time -> time)).getSum();

//                BigDecimal hour = new BigDecimal(60);
                BigDecimal hour = new BigDecimal(1);
                timeProfile.setWorkingHours((zuoYe == 0l) ? BigDecimal.ZERO : new BigDecimal(zuoYe + "").divide(hour, 2, RoundingMode.HALF_UP));
                timeProfile.setDischargingTime((paiLiao == 0l) ? BigDecimal.ZERO : new BigDecimal(paiLiao + "").divide(hour, 2, RoundingMode.HALF_UP));
                timeProfile.setCleaningTime((qingSao == 0l) ? BigDecimal.ZERO : new BigDecimal(qingSao + "").divide(hour, 2, RoundingMode.HALF_UP));
                timeProfile.setMaintenanceTime((xuanXiang == 0l) ? BigDecimal.ZERO : new BigDecimal(xuanXiang + "").divide(hour, 2, RoundingMode.HALF_UP));
                timeProfile.setMeetingTime((kaiHui == 0l) ? BigDecimal.ZERO : new BigDecimal(kaiHui + "").divide(hour, 2, RoundingMode.HALF_UP));
                timeProfile.setTrainingTime((peiXun == 0l) ? BigDecimal.ZERO : new BigDecimal(peiXun + "").divide(hour, 2, RoundingMode.HALF_UP));
                timeProfile.setOtherTime((qiTa == 0l) ? BigDecimal.ZERO : new BigDecimal(qiTa + "").divide(hour, 2, RoundingMode.HALF_UP));
            }
            //实际出勤人数
            timeProfile.setAttendance(stringObjectMap.containsKey("realAttendance") ? Integer.parseInt(stringObjectMap.get("realAttendance").toString()) : 0);

            attendanceTotal = attendanceTotal.add(new BigDecimal(timeProfile.getAttendance()));

//            BigDecimal one = this.getHour2(workTimeMap, "startTime1", "endTime1");
//
//            BigDecimal two = this.getHour2(workTimeMap, "startTime2", "endTime2");
//
//            BigDecimal three = this.getHour2(workTimeMap, "startTime3", "endTime3");
//
//            BigDecimal four = this.getHour2(workTimeMap, "startTime4", "endTime4");
//
//            BigDecimal five = this.getHour2(workTimeMap, "startTime5", "endTime5");
//
//            BigDecimal six = this.getHour2(workTimeMap, "startTime6", "endTime6");
//
//            BigDecimal seven = this.getHour2(workTimeMap, "startTime7", "endTime7");
//
//            BigDecimal eight = this.getHour2(workTimeMap, "startTime8", "endTime8");
//
//            BigDecimal nine = this.getHour2(workTimeMap, "startTime9", "endTime9");
//
//            BigDecimal ten = this.getHour2(workTimeMap, "startTime10", "endTime10");

//            BigDecimal workingHours = BigDecimal.ZERO;
//            workingHours = workingHours.add(one).add(three).add(five).add(seven).add(nine).setScale(2,
//                BigDecimal.ROUND_HALF_UP);
////            workingHours = workingHours.add(one).add(two).add(three).add(four).add(five).setScale(2,
////                BigDecimal.ROUND_HALF_UP);
//
//            BigDecimal dischargingTime = BigDecimal.ZERO;
//            dischargingTime = dischargingTime.add(two).setScale(2, BigDecimal.ROUND_HALF_UP);
//
//            BigDecimal cleaningTime = BigDecimal.ZERO;
//            cleaningTime = cleaningTime.add(four).setScale(2, BigDecimal.ROUND_HALF_UP);
//
//            BigDecimal maintenanceTime = BigDecimal.ZERO;
//            maintenanceTime = maintenanceTime.add(six).setScale(2, BigDecimal.ROUND_HALF_UP);
//
//            BigDecimal meetingTime = BigDecimal.ZERO;
//            meetingTime = meetingTime.add(eight).setScale(2, BigDecimal.ROUND_HALF_UP);
//
//            BigDecimal trainingTime = BigDecimal.ZERO;
//            trainingTime = trainingTime.add(ten).setScale(2, BigDecimal.ROUND_HALF_UP);
//
//            //作业时间
//            timeProfile.setWorkingHours(workingHours);
//            //排料时间
//            timeProfile.setDischargingTime(dischargingTime);
//            //清扫时间
//            timeProfile.setCleaningTime(cleaningTime);
//            //维修时间
//            timeProfile.setMaintenanceTime(maintenanceTime);
//            //开会时间
//            timeProfile.setMeetingTime(meetingTime);
//            //培训时间
//            timeProfile.setTrainingTime(trainingTime);
//            //作业时间
//            timeProfiles.add(timeProfile);
            //组装数量
            AssemblyDaily2Dto daily = detailMapById.get(stringObjectMap.get("id"));
            List<AssemblyDailyDetailDtO> dailyDetails = daily.getDailyDetails();
            BigDecimal totalMoldedQty = BigDecimal.ZERO;
            BigDecimal inspectionQty = BigDecimal.ZERO;
            BigDecimal qualifiedQty = BigDecimal.ZERO;

            BigDecimal qualifiedQty2 = BigDecimal.ZERO;

            BigDecimal inspectionQty2 = BigDecimal.ZERO;
            if (ObjectUtils.isEmpty(dailyDetails)) continue;
            badForm = new BadForm();

            for (int i = 0; i < dailyDetails.size(); i++) {

                if (ObjectUtil.isNotEmpty(dailyDetails.get(i).getUndeliveredCause()) && ObjectUtil.isNotEmpty(dailyDetails.get(i).getInspectionQty())) {
                    badForm.setBadItemKey(dailyDetails.get(i).getUndeliveredCause());
                    badForm.setBadItemValue(dailyDetails.get(i).getInspectionQty());
                    if (badFormMap.containsKey(badForm.getBadItemKey())) {
                        BadForm badForm1 = badFormMap.get(badForm.getBadItemKey());
                        badForm1.setBadItemValue(badForm1.getBadItemValue().add(badForm.getBadItemValue()));
                    } else {
                        badFormMap.put(badForm.getBadItemKey(), badForm);
                    }
                    //badForms.add(badForm);
                }
                totalMoldedQty = ObjectUtils.isEmpty(dailyDetails.get(i).getProductionQty()) ? BigDecimal.ZERO : dailyDetails.get(i).getProductionQty();
                inspectionQty = ObjectUtils.isEmpty(dailyDetails.get(i).getInspectionQty()) ? BigDecimal.ZERO : dailyDetails.get(i).getInspectionQty();
                qualifiedQty = ObjectUtils.isEmpty(dailyDetails.get(i).getQualifiedQty()) ? BigDecimal.ZERO : dailyDetails.get(i).getQualifiedQty();
                totalMoldedQtyTotal = totalMoldedQtyTotal.add(totalMoldedQty).setScale(AslConstant.ZERO,
                        BigDecimal.ROUND_HALF_UP);
                inspectionQtyTotal = inspectionQtyTotal.add(inspectionQty).setScale(AslConstant.ZERO,
                        BigDecimal.ROUND_HALF_UP);
                qualifiedQtyTotal = qualifiedQtyTotal.add(qualifiedQty).setScale(AslConstant.ZERO,
                        BigDecimal.ROUND_HALF_UP);

                qualifiedQty2 = qualifiedQty2.add(qualifiedQty);

                inspectionQty2 = inspectionQty2.add(inspectionQty);
                String feedingMaterial = String.valueOf(dailyDetails.get(i).getFeedingMaterial());
                JSONArray feedingMaterialArray = JSON.parseArray(feedingMaterial);
                BadForm ss = new BadForm();
                if (ObjectUtils.isEmpty(feedingMaterialArray)) continue;
                //style-子件不良类型；bl-子件不良数量；planFeeding-子件使用数量
//                for (Object o : feedingMaterialArray) {
//                    JSONObject jsonObject = JSON.parseObject(o.toString());
//                    if (jsonObject.containsKey("style") && !ObjectUtils.isEmpty(jsonObject.get("style")) && jsonObject.containsKey("bl") && !ObjectUtils.isEmpty(jsonObject.get("bl"))) {
//                        style = String.valueOf(jsonObject.get("style"));
//                        bl = String.valueOf(jsonObject.get("bl"));
//                        if (StringUtils.isNotBlank(style) && StringUtils.isNotBlank(bl)) {
////                        Integer keyId = Integer.parseInt(style);
////                        if (collect.containsKey(keyId)){
////                        checkProjectDO = collect.get(keyId);
////                        badForm.setBadItemKeyId(keyId);
//                            badForm.setBadItemKey(style);
//                            if (!ObjectUtils.isEmpty(badForms) && badForms.size()>0){
//                                List<BadForm> list = badForms.stream().filter(bad -> bad.getBadItemKey().equals(String.valueOf(jsonObject.get("style")))).collect(Collectors.toList());
//
//                                badForm.setBadItemValue(list.get(0).getBadItemValue().add(new BigDecimal(bl)));
//                            }else {
//                                badForm.setBadItemValue(new BigDecimal(bl));
//                            }
//                            badItemValueTotal = badItemValueTotal.add(badForm.getBadItemValue());
////                        }
//                        }
//                    }
//
//                    if (badForm.equals(ss)) continue;
//                    badForms.add(badForm);
//                }
            }
            //合格数量
            timeProfile.setQualifiedQty(qualifiedQty2);
            //不良数量
            timeProfile.setInspectionQty(inspectionQty2);
            timeProfiles.add(timeProfile);
//            BigDecimal totalMoldedQty = new BigDecimal(String.valueOf(
//                stringObjectMap.get("productionQty") != null ? stringObjectMap.get("productionQty")
//                    : "0"));
//            totalMoldedQtyTotal = totalMoldedQtyTotal.add(totalMoldedQty).setScale(AslConstant.ZERO,
//                BigDecimal.ROUND_HALF_UP);
            //套件不良数量
//            BigDecimal inspectionQty = new BigDecimal(String.valueOf(
//                stringObjectMap.get("inspectionQty") != null ? stringObjectMap.get("inspectionQty")
//                    : "0"));
//            inspectionQtyTotal = inspectionQtyTotal.add(inspectionQty).setScale(AslConstant.ZERO,
//                BigDecimal.ROUND_HALF_UP);
            //合格数量
//            BigDecimal qualifiedQty = new BigDecimal(String.valueOf(
//                stringObjectMap.get("qualifiedQty") != null ? stringObjectMap.get("qualifiedQty")
//                    : "0"));
//            qualifiedQtyTotal = qualifiedQtyTotal.add(qualifiedQty).setScale(AslConstant.ZERO,
//                BigDecimal.ROUND_HALF_UP);


//            Set<Integer> keyIds = new HashSet<>();
            //不良类型改成了手输，没有关联表id了
//            for (Object o : feedingMaterialArray) {
//                JSONObject jsonObject = JSON.parseObject(o.toString());
//                if (!ObjectUtils.isEmpty(jsonObject) && jsonObject.containsKey("style") && !ObjectUtils.isEmpty(jsonObject.get("style"))){
//                    Integer keyId = Integer.parseInt(String.valueOf(jsonObject.get("style")));
//                    keyIds.add(keyId);
//                }
//            }

//            Map<String, Object> stringObjectMap1 = new HashMap<>();
//            stringObjectMap1.put("ids", keyIds);
//            List<CheckProjectDO> checkProjectDOList = checkProjectService.list(stringObjectMap1);
//            Map<Long, CheckProjectDO> collect = checkProjectDOList.stream().collect(Collectors.toMap(CheckProjectDO::getId, v -> v, (v1, v2) -> v1));
//            String feedingMaterial = String.valueOf(stringObjectMap.get("feedingMaterial"));
//            JSONArray feedingMaterialArray = JSON.parseArray(feedingMaterial);
//            BadForm ss = new BadForm();
//            if(!ObjectUtils.isEmpty(feedingMaterialArray)){
//
//                for (Object o : feedingMaterialArray) {
//                    badForm = new BadForm();
//                    JSONObject jsonObject = JSON.parseObject(o.toString());
////                if (jsonObject.containsKey("bl") && !ObjectUtils.isEmpty(jsonObject.get("bl"))) {
////                    bl = String.valueOf(jsonObject.get("bl"));
////                    if (StringUtils.isNotBlank(bl)){
////                        badForm.setBadItemValue(new BigDecimal(bl));
////                        badItemValueTotal = badItemValueTotal.add(badForm.getBadItemValue());
////                    }
////                }
//                    if (jsonObject.containsKey("style") && !ObjectUtils.isEmpty(jsonObject.get("style")) && jsonObject.containsKey("bl") && !ObjectUtils.isEmpty(jsonObject.get("bl"))) {
//                        style = String.valueOf(jsonObject.get("style"));
//                        bl = String.valueOf(jsonObject.get("bl"));
//                        if (StringUtils.isNotBlank(style) && StringUtils.isNotBlank(bl)) {
////                        Integer keyId = Integer.parseInt(style);
////                        if (collect.containsKey(keyId)){
////                        checkProjectDO = collect.get(keyId);
////                        badForm.setBadItemKeyId(keyId);
//                            badForm.setBadItemKey(style);
//                            if (!ObjectUtils.isEmpty(badForms) && badForms.size()>0){
//                                List<BadForm> list = badForms.stream().filter(bad -> bad.getBadItemKey().equals(String.valueOf(jsonObject.get("style")))).collect(Collectors.toList());
//
//                                badForm.setBadItemValue(list.get(0).getBadItemValue().add(new BigDecimal(bl)));
//                            }else {
//                                badForm.setBadItemValue(new BigDecimal(bl));
//                            }
//                            badItemValueTotal = badItemValueTotal.add(badForm.getBadItemValue());
////                        }
//                        }
//                    }
//
//                    if (badForm.equals(ss)) continue;
//                    badForms.add(badForm);
////            for (Object o : feedingMaterialArray) {
////                BadForm badForm = new BadForm();
////                JSONObject jsonObject = JSON.parseObject(o.toString());
////                Integer keyId = Integer.parseInt(String.valueOf(jsonObject.get("style")));
////                for (CheckProjectDO checkProjectDO : checkProjectDOList) {
////                    if (keyId.equals(checkProjectDO.getId().intValue())) {
////                        badForm.setBadItemKeyId(keyId);
////                        badForm.setBadItemKey(checkProjectDO.getName());
////                    }
////                }
////                Integer keyId = Integer.parseInt(String.valueOf(jsonObject.get("style")));
////                for (CheckProjectDO checkProjectDO : checkProjectDOList) {
////                    if (keyId.equals(checkProjectDO.getId().intValue())) {
////                        badForm.setBadItemKeyId(keyId);
////                        badForm.setBadItemKey(checkProjectDO.getName());
////                    }
////                }
////                badForm.setBadItemValue(new BigDecimal(String.valueOf(jsonObject.get("bl"))));
////                badItemValueTotal = badItemValueTotal.add(badForm.getBadItemValue());
//                }
//            }
        }
//        totalMoldedQtyTotal = totalMoldedQtyTotal.add(bigDecimal);
        if (totalMoldedQtyTotal.compareTo(new BigDecimal(0)) <= 0) {
            yield = new BigDecimal(0);
        } else {
            yield = qualifiedQtyTotal
                    .divide(totalMoldedQtyTotal, AslConstant.TWO, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal("100"));
        }
//        yield = qualifiedQtyTotal
//            .divide(totalMoldedQtyTotal, AslConstant.TWO, BigDecimal.ROUND_HALF_UP)
//            .multiply(new BigDecimal("100"));
        // 生产实绩概况：组装数量-totalMoldedQty；合格数量-qualifiedQty；套装件不良数量-suiteInspectionQty；自件不良数量-compInspectionQty；良品率-
        ActualSituation actualSituation = new ActualSituation();
        actualSituation.setTotalMoldedQty(totalMoldedQtyTotal);
        actualSituation.setQualifiedQty(qualifiedQtyTotal);
        actualSituation.setSuiteInspectionQty(inspectionQtyTotal);
        actualSituation.setCompInspectionQty(badItemValueTotal);
        actualSituation.setYield(yield);
//
//        List<BadForm> badFormss = new ArrayList<>(badForms.stream().filter(o -> !ObjectUtils.isEmpty(o.getBadItemKeyId()) && !ObjectUtils.isEmpty(o.getBadItemValue()))
//            .collect(Collectors.toMap(BadForm::getBadItemKeyId, a -> a, (o1, o2) -> {
//                o1.setBadItemValue(o1.getBadItemValue().add(o2.getBadItemValue()));
//                return o1;
//            })).values());

        BigDecimal workingHoursTtl = BigDecimal.ZERO;
        BigDecimal dischargingTimeTtl = BigDecimal.ZERO;
        BigDecimal cleaningTimeTtl = BigDecimal.ZERO;
        BigDecimal maintenanceTimeTtl = BigDecimal.ZERO;
        BigDecimal meetingTimeTtl = BigDecimal.ZERO;
        BigDecimal trainingTimeTtl = BigDecimal.ZERO;
        BigDecimal otherTimeTtl = BigDecimal.ZERO;
        BigDecimal ttlTimeTtl = BigDecimal.ZERO;
        for (TimeProfile timeProfile : timeProfiles) {
            BigDecimal ttlTime = BigDecimal.ZERO;
            ttlTime = ttlTime
                    .add(timeProfile.getCleaningTime() != null ? timeProfile.getCleaningTime()
                            : BigDecimal.ZERO);
            ttlTime = ttlTime
                    .add(timeProfile.getDischargingTime() != null ? timeProfile.getDischargingTime()
                            : BigDecimal.ZERO);
            ttlTime = ttlTime
                    .add(timeProfile.getMaintenanceTime() != null ? timeProfile.getMaintenanceTime()
                            : BigDecimal.ZERO);
            ttlTime = ttlTime
                    .add(timeProfile.getMeetingTime() != null ? timeProfile.getMeetingTime()
                            : BigDecimal.ZERO);
            ttlTime = ttlTime
                    .add(timeProfile.getTrainingTime() != null ? timeProfile.getTrainingTime()
                            : BigDecimal.ZERO);
            ttlTime = ttlTime
                    .add(timeProfile.getWorkingHours() != null ? timeProfile.getWorkingHours()
                            : BigDecimal.ZERO);
            ttlTime = ttlTime
                    .add(timeProfile.getOtherTime() != null ? timeProfile.getOtherTime()
                            : BigDecimal.ZERO);
            timeProfile.setTtlTime(ttlTime);

            workingHoursTtl = workingHoursTtl
                    .add(timeProfile.getWorkingHours() != null ? timeProfile.getWorkingHours()
                            : BigDecimal.ZERO);
            dischargingTimeTtl = dischargingTimeTtl
                    .add(timeProfile.getDischargingTime() != null ? timeProfile.getDischargingTime()
                            : BigDecimal.ZERO);
            cleaningTimeTtl = cleaningTimeTtl
                    .add(timeProfile.getCleaningTime() != null ? timeProfile.getCleaningTime()
                            : BigDecimal.ZERO);
            maintenanceTimeTtl = maintenanceTimeTtl
                    .add(timeProfile.getMaintenanceTime() != null ? timeProfile.getMaintenanceTime()
                            : BigDecimal.ZERO);
            meetingTimeTtl = meetingTimeTtl
                    .add(timeProfile.getMeetingTime() != null ? timeProfile.getMeetingTime()
                            : BigDecimal.ZERO);
            trainingTimeTtl = trainingTimeTtl
                    .add(timeProfile.getTrainingTime() != null ? timeProfile.getTrainingTime()
                            : BigDecimal.ZERO);
            otherTimeTtl = otherTimeTtl
                    .add(timeProfile.getOtherTime() != null ? timeProfile.getOtherTime()
                            : BigDecimal.ZERO);
            ttlTimeTtl = ttlTimeTtl
                    .add(timeProfile.getTtlTime() != null ? timeProfile.getTtlTime() : BigDecimal.ZERO);
        }
        assemblyProductionStatisticsVo.setWorkingHoursTtl(workingHoursTtl);
        assemblyProductionStatisticsVo.setDischargingTimeTtl(dischargingTimeTtl);
        assemblyProductionStatisticsVo.setCleaningTimeTtl(cleaningTimeTtl);
        assemblyProductionStatisticsVo.setMaintenanceTimeTtl(maintenanceTimeTtl);
        assemblyProductionStatisticsVo.setMeetingTimeTtl(meetingTimeTtl);
        assemblyProductionStatisticsVo.setTrainingTimeTtl(trainingTimeTtl);
        assemblyProductionStatisticsVo.setOtherTimeTtl(otherTimeTtl);
        assemblyProductionStatisticsVo.setTtlTimeTtl(ttlTimeTtl);
        assemblyProductionStatisticsVo.setTimeProfiles(timeProfiles);
        assemblyProductionStatisticsVo.setActualSituation(actualSituation);
        assemblyProductionStatisticsVo.setAttendanceTotal(attendanceTotal);
        badForms = badFormMap.values().stream().collect(Collectors.toList());
        assemblyProductionStatisticsVo.setBadForms(badForms);
        BigDecimal badFormQtyTotal = BigDecimal.ZERO;
        for (BadForm badForm2 : badForms) {
            if (ObjectUtils.isEmpty(badForm2.getBadItemValue())) continue;
            badFormQtyTotal = badFormQtyTotal.add(badForm2.getBadItemValue());
        }
        assemblyProductionStatisticsVo.setBadFormQtyTotal(badFormQtyTotal);

        return assemblyProductionStatisticsVo;
    }

    private BigDecimal getHour(Map<String, Object> workTimeMap, String start, String end) {
        String stringNull = "";
        BigDecimal hourInterval = BigDecimal.ZERO;
        if ((Objects.nonNull(workTimeMap.get(start))
                && !stringNull.equals(String.valueOf(workTimeMap.get(start))))
                && (Objects.nonNull(workTimeMap.get(end))
                && !stringNull.equals(String.valueOf(workTimeMap.get(end))))) {
            Date startTime = DateUtils.getDate(String.valueOf(workTimeMap.get(start)));
            Date endTime = DateUtils.getDate(String.valueOf(workTimeMap.get(end)));
            hourInterval = new BigDecimal(
                    String.valueOf(DateUtils.getHourInterval(startTime, endTime)));
        }
        return hourInterval;
    }

    private BigDecimal getHour2(Map<String, Object> workTimeMap, String start, String end) {
        String stringNull = "";
        BigDecimal hourInterval = BigDecimal.ZERO;
        if ((Objects.nonNull(workTimeMap.get(start))
                && !stringNull.equals(String.valueOf(workTimeMap.get(start))))
                && (Objects.nonNull(workTimeMap.get(end))
                && !stringNull.equals(String.valueOf(workTimeMap.get(end))))) {
            Date startTime = DateUtil.parse(workTimeMap.get(end).toString());
            Date endTime = DateUtil.parse(workTimeMap.get(start).toString());
            hourInterval = new BigDecimal(
                    String.valueOf(DateUtil.between(startTime, endTime, DateUnit.HOUR)));
        }
        return hourInterval;
    }
}
